/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import connexiondatabase.MySQLManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hippolyte
 */
public class Panier extends HashMap<Legume, Integer> {

    MySQLManager sql = new MySQLManager("Potager");
    public String nom;
    public int prixTotal;
    private String recapitulatif;
    
    private String panierConnu;

    public Panier(String unNom) {
        super();
        nom = unNom;
        prixTotal = 0;
        recapitulatif = "";
        panierConnu = "";
    }

    public String getNom() {
        return nom;
    }

    public String getRecapitulatif() {
        return recapitulatif;
    }
    
    public String getPannierConnu() {
        return panierConnu;
    }

    public int getPrixTotal() {
        return prixTotal;
    }

    public void calculPanier() {
        recapitulatif = "";
        prixTotal = 0;
        for (Legume l : this.keySet()) {
            recapitulatif += l.getNom() + " quantité demandée : " + this.get(l)
                    + " ==> " + (this.get(l) * l.getPrix() + " €"
                    + " <input type=\"submit\" name=\"bt_ret1_" + l.getNom()
                    + "\" value=\"-\"/>"
                    + "<input type=\"submit\" name=\"bt_retTout_" + l.getNom()
                    + "\" value=\"Retirer\"/> " + "<br/>");
            prixTotal += this.get(l) * l.getPrix();
        }
    }

    public Legume getLegume(String nom) {
        for (Legume l : this.keySet()) {
            if (l.getNom().equalsIgnoreCase(nom)) {
                return l;
            }
        }
        return null;
    }

    public void ajouter(Legume l, int qte) {

        if (!this.containsKey(l)) {
            ajoutLegumePanierInDb(l, qte);
        } else {
            updatePanierInDb(l, qte);

            int qteActuelle = this.get(l);
            qte += qteActuelle;
            this.remove(l);

        }
        System.out.println("ajoute au panier de " + getNom() + " "
                + qte + " " + l.getNom());
        this.put(l, qte);
    }

    public void retirer(Legume l, int qte) {
        int oldQte = this.get(l);
        qte = oldQte - qte;
        updatePanierInDb(l, qte);
        this.remove(l);
        if (qte > 0) {
            this.put(l, qte);
        }
        calculPanier();
    }

    public boolean updatePanierInDb(Legume l, int qte) {

        if (qte <= 0) {
            return deletePanierInDb(l);
        }

        if (!sql.connect()) {
            return false;
        }


        Connection conn = sql.getConnection();
        try {
            Statement s = conn.createStatement();
            String query = "update panier set qte = " + qte
                    + " where nomLegume = '" + l.getNom() + "'"
                    + " and nomPanier = '" + nom + "'";
            System.out.println(query);
            s.executeUpdate(query);

        } catch (SQLException ex) {
            Logger.getLogger(Jardin.class.getName()).log(Level.SEVERE, null, ex);
            sql.disconnect();
            return false;
        }
        sql.disconnect();
        return true;
    }

    private boolean deletePanierInDb(Legume l) {

        if (!sql.connect()) {
            return false;
        }


        Connection conn = sql.getConnection();
        try {
            Statement s = conn.createStatement();

            String query = "delete from panier where nomLegume = '" + l.getNom() + "'"
                    + " and nomPanier = '" + this.nom + "'";
            System.out.println(query);
            s.executeUpdate(query);

        } catch (SQLException ex) {
            Logger.getLogger(Jardin.class.getName()).log(Level.SEVERE, null, ex);
            sql.disconnect();
            return false;
        }
        sql.disconnect();
        return true;
    }

    private boolean ajoutLegumePanierInDb(Legume l, int qte) {

        if (!sql.connect()) {
            return false;
        }


        Connection conn = sql.getConnection();
        try {
            Statement s = conn.createStatement();

            String query = "insert into panier (nomPanier, nomLegume, qte) values ( '"
                    + nom + "','" + l.getNom() + "', '" + qte + "')";
            System.out.println(query);
            s.executeUpdate(query);

        } catch (SQLException ex) {
            Logger.getLogger(Jardin.class.getName()).log(Level.SEVERE, null, ex);
            sql.disconnect();
            return false;
        }
        sql.disconnect();
        return true;
    }
    
    public boolean initPanierSQL() {

        if (!sql.connect()) {
            return false;
        }

        Legume l;

        Connection conn = sql.getConnection();
        try {
            Statement s = conn.createStatement();

            String query = "select p.nomLegume, qte, prix "
                    + "from panier p, legume l "
                    + "where l.nomLegume = p.nomLegume "
                    + "and nomPanier = '" + nom + "'";
            System.out.println(query);
            s.executeQuery(query);

            ResultSet rs = s.getResultSet();

            while (rs.next()) {
                l = new Legume(rs.getString("p.nomLegume"), rs.getInt("prix"));
                this.put(l, rs.getInt("qte"));
                System.out.println("retrouve " + l.getNom() + " "
                        + l.getPrix() + " " + rs.getInt("qte"));
            }


        } catch (SQLException ex) {
            Logger.getLogger(Jardin.class.getName()).log(Level.SEVERE, null, ex);
            sql.disconnect();
            return false;
        }
        sql.disconnect();
        if (this != null && !this.isEmpty()){
            panierConnu = "Votre panier n'est pas vide. Pour le consulter, "
                    + "<input type=\"submit\" name=\"bt_Consulter\" value=\"cliquez ici\">";
        }
        return true;
    }
}
